<?php
class Admin_MailController extends Zend_Controller_Action{
    private $_params;
	private $_auth;	
    public function init(){
        $this->_params      = $this->_request->getParams();        
        $this->_auth        = new My_Auth();
        $this->view->auth   = $this->_auth;        
        $this->_auth        = $this->_auth->checkAuthentication();
        $this->view->auth   = $this->_auth;
    }
    public function indexAction(){
        $modelM = new Admin_Model_Mail();
        $tempplate = $modelM->getAllData()->toArray();
        $this->view->template = $tempplate;        
    }
    public function editAction(){
        $form = new Admin_Form_Mail();
        $modelM = new Admin_Model_Mail();
		$form->submit->setLabel('Lưu thay đổi');
		$this->view->form = $form;
        $id = $this->_getParam('id','');        
        $template = $modelM->getDataId($id);        
		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
			if($form->isValid($formData)){
				$dataUpdate['id']       = $formData['id'];                				
                $dataUpdate['subject']  = $formData['subject'];
                $dataUpdate['name']     = $formData['name'];
                $dataUpdate['content']  = $formData['content'];				
                //$dataUpdate['comment']  = $formData['comment'];
                if($template){  
				    $modelM->updateData($id, $dataUpdate);
                    $this->_helper->redirector('index', 'mail', 'admin'); 
                }else{
                    $this->view->mess ="Không tồn tại tác vụ !!";
                    $this->view->flag = "alert";
    				$form->populate($formData);
    			}
			}else{
				$form->populate($formData);
			}
		}else{
		    $this->view->template = $template;  
            $form->populate($template->toArray());
		}
    }
}